package viewers;

import java.util.Date;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class TesteSC extends Composite {

	public TesteSC(Composite parent, int style) {
		super(parent, style);
		GridLayout layout= new GridLayout(1, false);
		this.setLayout(layout);
		this.setBackground(new Color(parent.getDisplay(),255,255,255));
	}

	public void appendMessage(String message) {
		Text label = new Text(this, SWT.READ_ONLY);
		label.setBackground(new Color(getDisplay(), 255,255,255));
		label.setText(new Date().toString()+message);
		label.setLayoutData(createGridData());
	}

	private GridData createGridData() {
		GridData fdNext = new GridData();
		fdNext.grabExcessHorizontalSpace = true;
		fdNext.horizontalAlignment = SWT.FILL;
		return fdNext;
	}

	public TableViewer appendTable(String message) {
		appendMessage(message);

		Table table = new Table(this, SWT.NONE);
		table.setLayoutData(createGridData());
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableViewer tableViewer = new TableViewer(table);
		tableViewer.setColumnProperties(new String[]{"column A"});

		TableColumn tc = new TableColumn(table, SWT.NONE);
		tc.setText("Column 1");
		tc.pack();

		tableViewer.setLabelProvider(new ITableLabelProvider() {

			@Override
			public void removeListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public void addListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public String getColumnText(Object element, int columnIndex) {
				// TODO Auto-generated method stub
				return element.toString();
			}

			@Override
			public Image getColumnImage(Object element, int columnIndex) {
				// TODO Auto-generated method stub
				return null;
			}
		});

		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setInput(new String[]{"Item A \nLinha 2","Item B","Item C"});
		tableViewer.setColumnProperties(new String[]{"col A"});

		table.getColumns();

		final int TEXT_MARGIN = 3;

		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				TableItem item = (TableItem)event.item;
				String text = item.getText(event.index);
				Point size = event.gc.textExtent(text);
				event.width = size.x + 2 * TEXT_MARGIN;
				event.height = Math.max(event.height, size.y + TEXT_MARGIN);
			}
		});

		table.addListener(SWT.EraseItem, new Listener() {
			public void handleEvent(Event event) {
				event.detail &= ~SWT.FOREGROUND;
			}
		});

		table.addListener(SWT.PaintItem, new Listener() {
			public void handleEvent(Event event) {
				TableItem item = (TableItem)event.item;
				String text = item.getText(event.index);
				/* center column 1 vertically */
				int yOffset = 0;
				if (event.index == 1) {
					Point size = event.gc.textExtent(text);
					yOffset = Math.max(0, (event.height - size.y) / 2);
				}
				event.gc.drawText(text, event.x + TEXT_MARGIN, event.y + yOffset,
						true);
			}
		});

		return tableViewer;



	}

	public void clear() {
		for (Control children : this.getChildren()){
			children.dispose();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Shell shell = new Shell(Display.getDefault());
		GridLayout gl =new GridLayout();
		shell.setLayout(gl);

		ScrolledComposite scomp = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		final TesteSC appender = new
				TesteSC(scomp, SWT.NONE);

		for (int i = 0; i < 3; i++) {
			appender.appendMessage("Message "+i);
			appender.appendTable("Message"+i);
			appender.appendMessage("Message"+i);
			appender.appendTable("Message"+i);
		}
		appender.setSize(500, 425);
		scomp.setContent(appender);
		scomp.setExpandHorizontal(true);
		scomp.setExpandVertical(true);
		scomp.setMinHeight(426);

		GridData gd = new GridData();
		gd.heightHint = 200;
		scomp.setLayoutData(gd);

		//usar gridlayout no pai do scrolledcomposite
		//setexpandvertical(true)
		//setMinHeight(), deve ser maior que o tamanho do content
		//setar altura do content de acordo com seu tamanho máximo real
		shell.setSize(300, 200);

		shell.open();

		while (!shell.isDisposed ()) {
			if (!shell.getDisplay().readAndDispatch ())
				shell.getDisplay().sleep();
		}


	}

}
